<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <EnableDefaultCompileItems>false</EnableDefaultCompileItems>
    <TargetFramework>$(NetCoreAppCurrent)</TargetFramework>
    <RuntimeIdentifier>android-$(TargetArchitecture)</RuntimeIdentifier>
    <SelfContained>true</SelfContained>
    <PublishTrimmed>true</PublishTrimmed>
    <TrimMode>Link</TrimMode>
    <ForceAOT Condition="'$(ForceAOT)' == ''">false</ForceAOT>
  </PropertyGroup>

  <ItemGroup>
    <RuntimeComponents Condition="'$(DiagnosticPorts)' != ''" Include="diagnostics_tracing" />
  </ItemGroup>

  <Import Project="$(RepoTasksDir)AotCompilerTask\MonoAOTCompiler.props" />
  <UsingTask TaskName="AndroidAppBuilderTask" 
             AssemblyFile="$(AndroidAppBuilderTasksAssemblyPath)"/>
  <UsingTask TaskName="MonoAOTCompiler" 
             AssemblyFile="$(MonoAOTCompilerTasksAssemblyPath)" />
  <UsingTask Condition="'$(RunAOTCompilation)' == 'true'"
             TaskName="NdkToolFinderTask"
             AssemblyFile="$(MobileBuildTasksAssemblyPath)" />
  
  <PropertyGroup Condition="'$(RunAOTCompilation)' == 'true'">
    <_MobileIntermediateOutputPath>$(IntermediateOutputPath)mobile</_MobileIntermediateOutputPath>
  </PropertyGroup>

  <Target Name="BuildApp" AfterTargets="CopyFilesToPublishDirectory">
    <PropertyGroup>
      <StripDebugSymbols>False</StripDebugSymbols>
      <StripDebugSymbols Condition="'$(Configuration)' == 'Release'">True</StripDebugSymbols>
      <ApkDir>$(PublishDir)apk\</ApkDir>
    </PropertyGroup>

    <ItemGroup>
      <AotInputAssemblies Include="$(PublishDir)\*.dll">
        <AotArguments>@(MonoAOTCompilerDefaultAotArguments, ';')</AotArguments>
        <ProcessArguments>@(MonoAOTCompilerDefaultProcessArguments, ';')</ProcessArguments>
      </AotInputAssemblies>
    </ItemGroup>

    <MakeDir Directories="$(_MobileIntermediateOutputPath)" 
             Condition="'$(ForceAOT)' == 'true'"/>
    <RemoveDir Directories="$(ApkDir)" />

    <Message Importance="High" Text="Path: $(PublishDir)" />
    <Message Importance="High" Text="SourceDir: $(OutputPath)" />

    <!-- Make sure marshal-ilgen is included in the components list. -->
    <ItemGroup>
      <RuntimeComponents Include="marshal-ilgen" KeepDuplicates="false"/>
    </ItemGroup>

    <PropertyGroup Condition="'$(ForceAOT)' == 'true' and '$(AOTWithLibraryFiles)' == 'true'">
      <_AotOutputType>Library</_AotOutputType>
      <_AotLibraryFormat>So</_AotLibraryFormat>
      <_AotMode Condition="'$(AotMode)' == ''">Normal</_AotMode>
    </PropertyGroup>

    <PropertyGroup Condition="'$(ForceAOT)' == 'true' and '$(AOTWithLibraryFiles)' != 'true'">
      <_AotOutputType>AsmOnly</_AotOutputType>
      <_AotModulesTablePath>$(ApkDir)\modules.c</_AotModulesTablePath>
      <_AotMode Condition="'$(AotMode)' == ''">Full</_AotMode>
    </PropertyGroup>

    <PropertyGroup>
      <AndroidLibraryMinApiLevel Condition="'$(AndroidLibraryMinApiLevel)' == ''">21</AndroidLibraryMinApiLevel>
    </PropertyGroup>

    <NdkToolFinderTask
      Condition="'$(AOTWithLibraryFiles)' == 'true'"
      Architecture="$(TargetArchitecture)"
      HostOS="$(_HostOS)"
      MinApiLevel="$(AndroidLibraryMinApiLevel)">
      <Output TaskParameter="AsPrefixPath" PropertyName="_AsPrefixPath" />
      <Output TaskParameter="ToolPrefixPath" PropertyName="_ToolPrefixPath" />
      <Output TaskParameter="Triple" PropertyName="_Triple" />
      <Output TaskParameter="LdName" PropertyName="_LdName" />
      <Output TaskParameter="LdPath" PropertyName="_LdPath" />
      <Output TaskParameter="ClangPath" PropertyName="_ClangPath" />
    </NdkToolFinderTask>

    <PropertyGroup Condition="'$(AOTWithLibraryFiles)' == 'true'">
      <_AsPrefixPath>$([MSBuild]::EnsureTrailingSlash('$(_AsPrefixPath)'))</_AsPrefixPath>
      <_ToolPrefixPath>$([MSBuild]::EnsureTrailingSlash('$(_ToolPrefixPath)'))</_ToolPrefixPath>
    </PropertyGroup>

    <MonoAOTCompiler Condition="'$(ForceAOT)' == 'true'"
        AotModulesTablePath="$(_AotModulesTablePath)"
        AsPrefix="$(_AsPrefixPath)"
        Assemblies="@(AotInputAssemblies)"
        CompilerBinaryPath="@(MonoAotCrossCompiler->WithMetadataValue('RuntimeIdentifier','$(TargetOS)-$(TargetArchitecture.ToLowerInvariant())'))"
        IntermediateOutputPath="$(IntermediateOutputPath)"
        LibraryFormat="$(_AotLibraryFormat)"
        LLVMPath="$(MonoAotCrossDir)"
        Mode="$(_AotMode)"
        OutputDir="$(_MobileIntermediateOutputPath)"
        OutputType="$(_AotOutputType)"
        ToolPrefix="$(_ToolPrefixPath)"
        UseAotDataFile="false"
        UseLLVM="$(UseLLVM)">
        <Output TaskParameter="CompiledAssemblies" ItemName="BundleAssemblies" />
    </MonoAOTCompiler>

    <AndroidAppBuilderTask
        AppDir="$(PublishDir)"
        Assemblies="@(BundleAssemblies)"
        DiagnosticPorts="$(DiagnosticPorts)"
        ExtraLinkerArguments="@(_ExtraAppLinkerArgs)"
        ForceInterpreter="$(MonoForceInterpreter)"
        ForceAOT="$(ForceAOT)"
        MainLibraryFileName="$(AssemblyName).dll"
        MonoRuntimeHeaders="$(MicrosoftNetCoreAppRuntimePackDir)runtimes\android-$(TargetArchitecture)\native\include\mono-2.0"
        OutputDir="$(ApkDir)"
        ProjectName="HelloAndroid"
        RuntimeComponents="@(RuntimeComponents)"
        RuntimeIdentifier="$(RuntimeIdentifier)"
        StripDebugSymbols="$(StripDebugSymbols)">
        <Output TaskParameter="ApkBundlePath" PropertyName="ApkBundlePath" />
        <Output TaskParameter="ApkPackageId" PropertyName="ApkPackageId" />
    </AndroidAppBuilderTask>

    <Message Importance="High" Text="Apk:       $(ApkBundlePath)"/>
    <Message Importance="High" Text="PackageId: $(ApkPackageId)"/>

    <Exec Condition="'$(DeployAndRun)' == 'true' and '$(ArchiveTests)' != 'true'" Command="dotnet xharness android test --package-name=net.dot.HelloAndroid --instrumentation=net.dot.MonoRunner --app=$(ApkDir)/bin/HelloAndroid.apk --expected-exit-code=42 --output-directory=$(ApkDir)/log" />
  </Target>

  <ItemGroup>
    <Compile Include="Program.cs" />
  </ItemGroup>

  <Target Name="CopySampleAppToHelixTestDir" 
          Condition="'$(ArchiveTests)' == 'true'" 
          AfterTargets="Build"
          DependsOnTargets="Publish;BuildApp" >
    <PropertyGroup>
      <!-- Helix properties -->
      <!-- AnyCPU as Platform-->
      <OSPlatformConfig>$(TargetOS).AnyCPU.$(Configuration)</OSPlatformConfig>
      <!-- <OSPlatformConfig>$(TargetOS).$(Platform).$(Configuration)</OSPlatformConfig> -->
      <HelixArchiveRoot>$(ArtifactsDir)helix/</HelixArchiveRoot>
      <HelixArchiveRunOnlyRoot>$(HelixArchiveRoot)runonly/</HelixArchiveRunOnlyRoot>
      <HelixArchiveRunOnlyAppsDir>$(HelixArchiveRunOnlyRoot)$(OSPlatformConfig)/</HelixArchiveRunOnlyAppsDir>
    </PropertyGroup>
    <ItemGroup>
      <_apkFiles Include="$(ApkBundlePath)" />
    </ItemGroup>
    <Copy SourceFiles="@(_apkFiles)"
          DestinationFolder="$(HelixArchiveRunOnlyAppsDir)/%(RecursiveDir)" />

    <Message Importance="High" Text="ApkBundlePath: $(ApkBundlePath)"/>
    <Message Importance="High" Text="TestArchiveTestsDir: $(TestArchiveTestsDir)"/>
  </Target>
</Project>
